package com.heima.leadnews.article.service.impl;

import com.heima.leadnews.article.service.ApArticleConfigService;
import com.heima.leadnews.article.service.ApArticleContentService;
import com.heima.model.article.dto.ApArticleDTO;
import com.heima.model.article.dto.ApArticleLoadMoreDTO;
import com.heima.model.article.dto.ApArticleLoadNewDTO;
import com.heima.model.article.pojos.ApArticle;
import com.heima.leadnews.article.mapper.ApArticleMapper;
import com.heima.leadnews.article.service.ApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmNewsDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-05-31
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigService configService;

    @Autowired
    private ApArticleContentService contentService;


    /**
     * 查询频道最新的十个文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<List<ApArticle>> load(ApArticleDTO dto) {
        List<ApArticle> apArticles = apArticleMapper.load(dto.getTag(), dto.getSize());
        return ResponseResult.okResult(apArticles);
    }

    /**
     * 上滑，查询比底部文章发布时间小的10个文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<List<ApArticle>> loadMore(ApArticleLoadMoreDTO dto) {
        List<ApArticle> apArticles = apArticleMapper.loadMore(dto.getTag(), dto.getSize(), dto.getMinBehotTime());
        return ResponseResult.okResult(apArticles);

    }

    /**
     * 下滑，查询比顶部文章发布时间大的10个文章
     *
     * @param dto
     * @return
     */

    @Override
    public ResponseResult<List<ApArticle>> loadNew(ApArticleLoadNewDTO dto) {
        List<ApArticle> apArticles = apArticleMapper.loadNew(dto.getTag(), dto.getSize(), dto.getMaxBehotTime());
        return ResponseResult.okResult(apArticles);

    }

    /**
     * 保存用户端文章的接口
     * 该接口是一个内部接口，被wemedia服务远程调用的。
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult<Long> saveArticle(WmNewsDTO dto) {
        //1 向《文章基本信息表》插入文章信息
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        apArticle.setAuthorId(dto.getUserId());
        apArticle.setLayout(dto.getType());
        apArticle.setFlag(0);
        apArticle.setLikes(0);
        apArticle.setCollection(0);
        apArticle.setComment(0);
        apArticle.setViews(0);
        save(apArticle);
        //2 向《文章配置表》插入文章信息
        Long apArticleId = apArticle.getId();
        ApArticleConfig config = new ApArticleConfig();
        config.setArticleId(apArticleId);
        config.setIsComment(true);
        config.setIsForward(true);
        config.setIsDown(false);
        config.setIsDelete(false);
        configService.save(config);
        //3 向《文章关联表》插入文章信息
        ApArticleContent content = new ApArticleContent();
        content.setArticleId(apArticleId);
        content.setContent(dto.getContent());
        contentService.save(content);
        return ResponseResult.okResult(apArticleId);
    }

    /**
     * 更新文章详情页面地址
     *
     * @param articleId
     * @param url
     * @return
     */
    @Override
    public ResponseResult<Long> updateStaticUrl(Long articleId, String url) {
        ApArticle apArticle = getById(articleId);
        if (apArticle == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        apArticle.setStaticUrl(url);
        updateById(apArticle);
        return ResponseResult.okResult();
    }
}


















